//
//  ViewController.swift
//  Signup
//
//  Created by apple on 2018/9/19.
//  Copyright © 2018年 apple. All rights reserved.
//

import UIKit
import RxSwift
import RxCocoa

class ViewController: UIViewController {

    let disposeBag = DisposeBag()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.title = "RX Examples"
        self.view.addSubview(tableView)
        
        setup()
    }
    
    fileprivate func setup() {
        
        let items = Observable.just([
            "Sign Up",
            "Search"
            ])
        
        items
            .bind(to: tableView.rx.items) { (tableView, row, element) in
                
            let cell = tableView.dequeueReusableCell(withIdentifier: "cell")!
            cell.textLabel?.text = " \(element)"
            return cell
                
        }.disposed(by: disposeBag)
        
        
        tableView.rx.itemSelected.subscribe(onNext: { [weak self] indexPath in
            
            if indexPath.row == 0 {
                
                self?.navigationController?.pushViewController(SignUpViewController(), animated: true)
            }
            
            if indexPath.row == 1 {
                
                self?.navigationController?.pushViewController(GithubSearchViewController(), animated: true)
            }
            
        }).disposed(by: disposeBag)
        
        
    }
    
    override func viewWillLayoutSubviews() {
        super.viewWillLayoutSubviews()
        
        self.tableView.frame = self.view.bounds
        
    }
    
    //MARK: getter and setter
    fileprivate lazy var tableView: UITableView = {
        
        let tableView = UITableView()
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
        
        return tableView
    }()
}

